package com.jdwa.yzp.service.impl;

import com.jdwa.yzp.entity.User;
import com.jdwa.yzp.mapper.IUserMapper;
import com.jdwa.yzp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private IUserMapper iUserMapper;


    @Override
    public List<User> selectAll() {
        return iUserMapper.selectAll();
    }

    @Override
    public User selectUserById(Long id) {
        return iUserMapper.selectUserById(id);
    }

    @Override
    public User Login(String name, String password) {
        User user = iUserMapper.selectUserByName(name);
        if (user.getPassword().equals(password)){
            return user;
        }
        return null;
    }

    //假如用户名不能重复，通过用户名进行新增及修改
    @Override
    public int insertOrUpdate(User user) {
        User user1 = iUserMapper.selectUserByName(user.getName());
        if (user1 !=null ){
            user.setId(user1.getId());
            return iUserMapper.update(user);
        }else {
            return iUserMapper.insert(user);
        }
    }

    @Override
    public int delete(Long id) {
        return iUserMapper.delete(id);
    }
}
